package com.dao;

import com.entity.BoneTumor;
import com.util.DBpool.DBconnection;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import static com.util.GlobalArgs.base_index_boneTumor;

/**
 * Created by wlc on 2018/5/31.
 */
public class BoneTumorDao {
    public static boolean data_save(BoneTumor boneTumor){
        DBconnection saveInDB = new DBconnection();

        saveInDB.setSheet_name("BoneTumor_user_Information");
        if(!saveInDB.saveDataToDB(boneTumor.getBoneTumor_User_Information())) {
            return false;
        }

        saveInDB.setSheet_name("BoneTumor_visit_Information");
        if(!saveInDB.saveDataToDB(boneTumor.getBoneTumor_Visit_Information())) {
            return false;
        }

        saveInDB.setSheet_name("BoneTumor_check_information");
        if(!saveInDB.saveDataToDB(boneTumor.getBoneTumor_Check_Information())) {
            return false;
        }

        saveInDB.setSheet_name("BoneTumor_surgery_information");
        if(!saveInDB.saveDataToDB(boneTumor.getBoneTumor_Surgery_Information())) {
            return false;
        }

        saveInDB.setSheet_name("BoneTumor_followUp_information");
        ArrayList<ArrayList<String>> temp_followArray = boneTumor.getBoneTumor_Follow_Information();

        for (ArrayList<String> followPart : temp_followArray){
            if(!saveInDB.saveDataToDB(followPart)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 查询全部随访数据
     * @return
     */
    public static ArrayList<ArrayList<String>> load_all_data() {
        DBconnection savaInDB = new DBconnection();
        savaInDB.setSheet_name("BoneTumor_user_Information");
        ArrayList<String> table_list = new ArrayList<String>() {{
            add("BoneTumor_visit_Information");
            add("BoneTumor_check_information");
            add("BoneTumor_surgery_information");
            add("BoneTumor_followUp_information");
        }};
        return savaInDB.getFormDataJoinTable_2("id", table_list);
    }

    /**
     * 根据关键词搜索数据库骨肿瘤随访数据
     * @param base
     * @param keyword
     * @return
     */
    public static ArrayList<ArrayList<String>> searchQM_data_by_keyword(String base,String keyword){
        ArrayList<String> join_table_names = new ArrayList<String>(){{
            add("BoneTumor_visit_Information");
            add("BoneTumor_check_information");
            add("BoneTumor_surgery_information");
            add("BoneTumor_followUp_information");
        }};
        DBconnection saveInDB = new DBconnection();

//        String table_name = base_index_boneTumor.get(base).split("-")[0];
        String table_name = "BoneTumor_user_Information";
        String col_name = base_index_boneTumor.get(base).split("-")[1];         //查询字段
        saveInDB.setSheet_name(table_name);    //查询表
        ArrayList<ArrayList<String>> search_result;
        search_result = saveInDB.getFormDataJoinTable(col_name, "id", keyword, join_table_names);
        return search_result;
    }


    /**
     * 查询骨肿瘤表全部字段名称
     * @return
     */
    public static ArrayList<String> getTitleName(){

        ArrayList<String> table_names = new ArrayList<String>(){{
            add("BoneTumor_user_Information");
            add("BoneTumor_visit_Information");
            add("BoneTumor_check_information");
            add("BoneTumor_surgery_information");
            add("BoneTumor_followUp_information");
        }};

        DBconnection saveInDB = new DBconnection();

        saveInDB.setSheet_name("BoneTumor_user_Information");

        ArrayList<String> title = new ArrayList<>();
        for (int i = 0;i < table_names.size();i++){

            saveInDB.setSheet_name(table_names.get(i));
            ArrayList<String> temp_title = saveInDB.getTitleFromDB();
            if(i != 0){
                temp_title.remove(0);
            }
            title.addAll(temp_title);
        }
        return title;
    }

    /**
     * 查询骨肿瘤表导出数据表头名称
     * @return
     */
    public static ArrayList<String> getHeaderName() {
        DBconnection dBconnection = new DBconnection();
        dBconnection.setSheet_name("BoneTumor_export_header");

        return dBconnection.getDataFromFiled(1);
    }


}
